﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Text;
using Thor.Domain;

namespace Thor.Repository.EntFx
{
    public class InvalidLogonRepository : RepositoryBase, IInvalidLogonRepository
    {
        #region Constructors

        public InvalidLogonRepository(Dictionary<string, object> repositoryParameters)
            : base(repositoryParameters) { }

        #endregion

        #region IInvalidLogonRepository Members

        public void Insert(InvalidLogon invalidLogon)
        {
            if (invalidLogon != null && Model != null)
            {
                var entity = CreateEntityFromPoco(invalidLogon);
                Model.AddToInvalidLogonEntities(entity);
                Model.SaveChanges();
            }
        }

        public void Update(InvalidLogon invalidLogon)
        {
            if (invalidLogon != null && Model != null)
            {
                Delete(invalidLogon);
                Insert(invalidLogon);
            }
        }

        public void Delete(InvalidLogon invalidLogon)
        {
            if (invalidLogon != null && Model != null)
            {
                var entity = Model.InvalidLogonEntities.Where(i => i.Id == invalidLogon.Id).FirstOrDefault();

                if (entity != null)
                {
                    Model.DeleteObject(entity);
                    Model.SaveChanges();
                }
            }
        }

        public List<InvalidLogon> Fetch()
        {
            if (Model != null)
            {
                List<InvalidLogon> result = new List<InvalidLogon>();

                var entities = Model.InvalidLogonEntities.ToList();

                if (entities != null)
                {
                    foreach (var entity in entities)
                        result.Add(CreatePocoFromEntity(entity));
                }

                return result;
            }

            return null;
        }

        public InvalidLogon Fetch(Guid id)
        {
            if (Model != null)
            {
                var entity = Model.InvalidLogonEntities.Where(i => i.Id == id).FirstOrDefault();

                if (entity != null)
                    return CreatePocoFromEntity(entity);
                else
                    return null;
            }

            return null;
        }

        #endregion

        #region Methods

        private InvalidLogonEntity CreateEntityFromPoco(InvalidLogon poco)
        {
            InvalidLogonEntity entity = new InvalidLogonEntity();

            entity.ApplicationName = this.ApplicationName;
            entity.AttemptCount = poco.AttemptCount;
            entity.FirstAttemptTimestamp = poco.FirstAttemptTimestamp;
            entity.Id = poco.Id;
            entity.LastAttemptTimestamp = poco.LastAttemptTimestamp;
            entity.UserName = poco.UserName;

            return entity;
        }

        private InvalidLogon CreatePocoFromEntity(InvalidLogonEntity entity)
        {
            InvalidLogon poco = new InvalidLogon();

            poco.AttemptCount = entity.AttemptCount;
            poco.FirstAttemptTimestamp = entity.FirstAttemptTimestamp;
            poco.Id = entity.Id;
            poco.LastAttemptTimestamp = entity.LastAttemptTimestamp;
            poco.UserName = entity.UserName;

            return poco;
        }

        #endregion
    }
}